﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Jzy.Core.Common.Web;
using Jzy.Model.CRM;
using Jzy.BLL.CRM;
using Frame.Lib.Common;
using Frame.Lib.Utilities;
using Frame.Lib.Core.Dict;
using Frame.Lib.Enums;


namespace Jzy.Web.CRM.Visit
{
    public partial class Edit : PageEdit
    {
        private CustomerVisitBO _visit;
        private CustomerVisitBO Visit
        {
            get
            {
                if (_visit == null)
                {
                    _visit = new CustomerVisitBO();
                }
                return _visit;

            }
        }

        private string CustomerID
        {
            get
            {
                return Request["CustomerID"];
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DictBO dict = new DictBO();
                dict.BindListControl(ContactMethod, (int)DictItem.联系方式);
                if (!string.IsNullOrEmpty(PKID))
                {
                    DispInfo();
                }
                else
                {
                    InputTime.Text = DateTime.Now.ToString("yyyy-MM-dd");
                }
            }
            AddAttr();
            AjaxPro.Utility.RegisterTypeForAjax(typeof(Edit));
        }

        private void AddAttr()
        {
            this.txtNextTime.Attributes.Add("onclick", "setday(this,false)");
            this.InputTime.Attributes.Add("onclick", "setday(this,false)");

        }

        private void DispInfo()
        {
            CustomerVisitInfo info = this.Visit.GetInfo(this.PKID);

            EntityUtil.SetControl(pnlMain, info);

            InputTime.Text = info.InputTime.ToString("yyyy-MM-dd");

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            CustomerVisitInfo info = this.Visit.GetInfo(this.PKID);

            EntityUtil.SetEntity(pnlMain, info);

            if (string.IsNullOrEmpty(PKID))
            {
                info.CustomerID = int.Parse(this.CustomerID);
                info.UserID = AppSession.UserID;
                info.UserName = AppSession.UserName;                
            }

            if (InputTime.Text == "")
            {
                info.InputTime = DateTime.Now;
            }

            this.Visit.Save(info);


            if (txtNextTime.Text.Trim() != "")//只要有就更新
            {
                CustomerBO customer = new CustomerBO();
                CustomerInfo custInfo = customer.GetInfo(this.CustomerID);
                custInfo.NextContactTime = DateTime.Parse(txtNextTime.Text);
                customer.Save(custInfo);
            }
            else
            {
                if (string.IsNullOrEmpty(PKID))//如果是新增则更新客户表
                {
                    CustomerBO customer = new CustomerBO();
                    CustomerInfo custInfo = customer.GetInfo(this.CustomerID);
                    custInfo.NextContactTime = null;
                    customer.Save(custInfo);
                }
            }


           
            JSUtil.RefreshView();
        }

        [AjaxPro.AjaxMethod]
        public bool CheckTime(string strTime)
        {
            try
            {
                DateTime tmp = DateTime.Parse(strTime);
                return true;
            }
            catch (FormatException)
            {
                return false;
            }
        }
    }
}
